//package com.zzkj.utils;
//
//import java.util.List;
//import java.util.Map;
//import java.util.concurrent.TimeUnit;
//
//import org.apache.shiro.SecurityUtils;
//import org.apache.shiro.UnavailableSecurityManagerException;
//import org.apache.shiro.session.InvalidSessionException;
//import org.apache.shiro.session.Session;
//import org.apache.shiro.subject.Subject;
//import org.springframework.beans.BeansException;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.ApplicationContext;
//import org.springframework.context.ApplicationContextAware;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.stereotype.Service;
//
//import com.wangzhixuan.mapper.ResourceMapper;
//import com.wangzhixuan.mapper.UserMapper;
//import com.wangzhixuan.mapper.UserRoleMapper;
//import com.wangzhixuan.model.User;
//import com.zzkj.dao.TSysMenuMapper;
//import com.zzkj.dao.TSysOperationMapper;
//import com.zzkj.dao.TSysRoleMapper;
//import com.zzkj.dao.TSysUserMapper;
//import com.zzkj.dao.dto.resp.authorized.TSysMenuResp;
//import com.zzkj.dao.dto.resp.authorized.TSysOperationResp;
//import com.zzkj.dao.dto.resp.authorized.TSysRoleResp;
//import com.zzkj.dao.dto.resp.authorized.TSysUserResp;
//import com.zzkj.dao.model.TSysMenu;
//import com.zzkj.dao.model.TSysMenuExample;
//import com.zzkj.dao.model.TSysUser;
//import com.zzkj.dao.model.TSysUserExample;
//
//
//@Service
//public class UserUtils implements ApplicationContextAware  {
//
//	@Autowired
//	UserMapper sysUserMapper;
//	@Autowired
//	ResourceMapper sysOperationMapper;
//	@Autowired
//	UserRoleMapper sysRoleMapper;
//	@Autowired
//	TSysMenuMapper sysMenuMapper;
//
//	public static final String CACHE_ID_ = "id_";
//	public static final String USER_CACHE = "userCache";
//	public static final String USER_ROLE_CACHE = "userRoleCache";
//	public static final String MENU_LIST_CHCHE = "menuListCache";
//	public static final String PARENT_MENU_LIST_CHCHE = "parentMenuListCache";
//	public static final String OPERATION_LIST_CHCHE = "operationListCache";
//	private static RedisTemplate<String, Object> redisTemplate;
//
//	public User getUserByAccountNum(String accountNum) {
//		User user = (User) redisTemplate.boundHashOps(USER_CACHE).get(CACHE_ID_ + accountNum);
//		if (user == null) {
//			TSysUserExample example=new TSysUserExample();
//			example.createCriteria().andAccountNumEqualTo(accountNum);
//			List<TSysUser> list=sysUserMapper.selectByExample(example);
//			if(list!=null && list.size()>0) {
//				user =list.get(0);
//			}else {
//				example.createCriteria().andMobileEqualTo(accountNum);
//				list=sysUserMapper.selectByExample(example);
//				if(list!=null && list.size()>0) {
//					user =list.get(0);
//				}
//			}
//			redisTemplate.boundHashOps(USER_CACHE).put(CACHE_ID_ + accountNum, user);
//		}
//		return user;
//	}
//	
//	public List<TSysOperationResp> qryRoleMenuOper(String userId, String menuId, String roleId) {
//		TSysOperationResp operation = new TSysOperationResp();
//		operation.setMenuId(menuId);
//		operation.setRoleId(roleId);
//		@SuppressWarnings("unchecked")
//		List<TSysOperationResp> operList = (List<TSysOperationResp>) redisTemplate.boundHashOps(OPERATION_LIST_CHCHE).get(CACHE_ID_ + menuId + userId);
//		if (operList == null || operList.size() <= 0) {
//			operList =sysOperationMapper.qryRoleMenuOper(operation);
//			redisTemplate.boundHashOps(OPERATION_LIST_CHCHE).put(CACHE_ID_ + menuId + userId, operList);
//			redisTemplate.boundHashOps(OPERATION_LIST_CHCHE).expire(18000, TimeUnit.SECONDS);  
//		}
//		return operList;
//	}
//
//	/**
//	 * 根据ID获取用户
//	 * 
//	 * @param id
//	 * @return 取不到返回null
//	 */
//	public TSysUserResp get(String id) {
//		TSysUserResp user = (TSysUserResp) redisTemplate.boundHashOps(USER_CACHE).get(CACHE_ID_ + id);
//		if (user == null) {
//			user = sysUserMapper.getUserById(id);
//			if (user == null) {
//				return null;
//			}
//			redisTemplate.boundHashOps(USER_CACHE).put(CACHE_ID_ + id,user);
//
//			@SuppressWarnings("unchecked")
//			List<TSysRoleResp> roleList = (List<TSysRoleResp>) redisTemplate.boundHashOps(USER_ROLE_CACHE).get(CACHE_ID_ + user.getUserId());
//			if (roleList == null || roleList.size() <= 0) {
//				roleList = sysRoleMapper.getUserRole(user.getUserId());
//				redisTemplate.boundHashOps(USER_ROLE_CACHE).put(CACHE_ID_ + user.getUserId(), roleList);
//			}
//			user.setRoleList(roleList);
//		}
//		return user;
//	}
//
//	public TSysUserResp getUser() {
//		Principal principal = getPrincipal();
//		if (principal != null) {
//			TSysUserResp user = get(principal.getUserId());
//			if (user != null) {
//				user.setAppKey(Constants.ATB_APPKEY);
//				return user;
//			}
//			return new TSysUserResp();
//		}
//		// 如果没有登录，则返回实例化空的User对象。
//		return new TSysUserResp();
//	}
//
//	/**
//	 * 获取当前用户授权菜单
//	 * 
//	 * @return
//	 */
//	public List<TSysMenuResp> getMenuList() {
//		TSysUserResp user = getUser();
//		@SuppressWarnings("unchecked")
//		List<TSysMenuResp> menuList = (List<TSysMenuResp>) redisTemplate.boundHashOps(MENU_LIST_CHCHE).get(CACHE_ID_ + user.getUserId());
//		if (menuList == null || menuList.size() <= 0) {
//			menuList = sysMenuMapper.qryMenuByUserId(user);
//			redisTemplate.boundHashOps(MENU_LIST_CHCHE).put(CACHE_ID_ + user.getUserId(), menuList);
//			redisTemplate.boundHashOps(MENU_LIST_CHCHE).expire(18000, TimeUnit.SECONDS);  
//		}
//		return menuList;
//	}
//	/**
//	 * 获取当前用户授权菜单
//	 * 
//	 * @return
//	 */
//	public List<Map<String,String>> getParenMenuList() {
//		TSysUserResp user = getUser();
//		@SuppressWarnings("unchecked")
//		List<Map<String,String>> menuList = (List<Map<String,String>>) redisTemplate.boundHashOps(PARENT_MENU_LIST_CHCHE).get(CACHE_ID_ + user.getUserId());
//		if (menuList == null || menuList.size() <= 0) {
//			TSysMenuResp record=new TSysMenuResp();
//			record.setAppId("e69d93e8ef34440f870d9539fc4754e0");
//			record.setMenuLevel("1");
//			menuList = sysMenuMapper.qryAppHieghtUpMenu(record);
//			redisTemplate.boundHashOps(PARENT_MENU_LIST_CHCHE).put(CACHE_ID_ + user.getUserId(), menuList);
//			redisTemplate.boundHashOps(PARENT_MENU_LIST_CHCHE).expire(18000, TimeUnit.SECONDS);  
//		}
//		return menuList;
//	}
//
//	public static void removeAllCache() {
//		Principal principal = getPrincipal();
//		redisTemplate.boundHashOps(USER_CACHE).delete(CACHE_ID_ + principal.getUserId());
//		redisTemplate.boundHashOps(OPERATION_LIST_CHCHE).delete(CACHE_ID_ + principal.getUserId());
//		redisTemplate.boundHashOps(USER_ROLE_CACHE).delete(CACHE_ID_ + principal.getUserId());
//		redisTemplate.boundHashOps(MENU_LIST_CHCHE).delete(CACHE_ID_ + principal.getUserId());
//	}
//
//	/**
//	 * 获取授权主要对象
//	 */
//	public static Subject getSubject() {
//		return SecurityUtils.getSubject();
//	}
//
//	/**
//	 * 获取当前登录者对象
//	 */
//	public static Principal getPrincipal() {
//		try {
//			Subject subject = SecurityUtils.getSubject();
//			Principal principal = (Principal) subject.getPrincipal();
//			if (principal != null) {
//				return principal;
//			}
//		} catch (UnavailableSecurityManagerException e) {
//
//		} catch (InvalidSessionException e) {
//
//		}
//		return null;
//	}
//
//	public static Session getSession() {
//		try {
//			Subject subject = SecurityUtils.getSubject();
//			Session session = subject.getSession(false);
//			if (session == null) {
//				session = subject.getSession();
//			}
//			if (session != null) {
//				return session;
//			}
//		} catch (InvalidSessionException e) {
//		}
//		return null;
//	}
//
//	/* (non-Javadoc)
//	 * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
//	 */
//	@Override
//	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
//		// TODO Auto-generated method stub
//		redisTemplate = (RedisTemplate) applicationContext.getBean("redisTemplate");
//	}
//	
//	public static String getPermitted(String authMenu,boolean flage) {
//		Subject subject = SecurityUtils.getSubject();
//		authMenu=authMenu+":";
//		//add, cancel, del, save
//		StringBuffer toolbarItem=new StringBuffer();
//		if(subject.isPermitted(authMenu+"save")) { 
//			toolbarItem.append("add").append(",");
//		}
//		if(subject.isPermitted(authMenu+"update")) {
//			toolbarItem.append("edit").append(",");
//			if(flage) {
//				toolbarItem.append("refresh").append(",");
//				toolbarItem.append("cancel").append(",");
//				toolbarItem.append("save").append(",");
//			}
//		}
//		if(subject.isPermitted(authMenu+"delete")) {
//			toolbarItem.append("del").append(",");
//		}
//		if(subject.isPermitted(authMenu+"import")) {
//			toolbarItem.append("import").append(",");
//		}
//		if(subject.isPermitted(authMenu+"export")) {
//			toolbarItem.append("export").append(",");
//		}
//		if(subject.isPermitted(authMenu+"exportSelect")) {
//			toolbarItem.append("exportf").append(",");
//		}
//		if(toolbarItem.length()>0) {
//			toolbarItem.deleteCharAt(toolbarItem.length()-1);
//		}
//		return toolbarItem==null?null:toolbarItem.toString();
//	}
//}
